package com.primb.baselibs.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Chen on 2016/12/7.
 * 功能描述：数据格式化工具类
 */

public class FormatUtils {

    public static final DecimalFormat FLOAT_FORMAT = new DecimalFormat("###,###,###,##0.0");
    public static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("###,###,###,##0.00");
    public static final DecimalFormat NUMBER_FORMAT = new DecimalFormat("###,###,###,##0.0000");

    public static final DecimalFormat INT_FORMAT = new DecimalFormat("###,###,###,###");

    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static final SimpleDateFormat NO_CHAR_FORMAT = new SimpleDateFormat("yyyyMMdd");

    /**
     * @param date yyyyMMdd
     * @return
     */
    public static String formatDate(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateStr;
        try {
            dateStr = DATE_FORMAT.format(dateFormat.parse(date));
        } catch (ParseException e) {
            dateStr = date;
        }
        return dateStr;
    }

    public static String formatDate(Date date, String flag) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy" + flag + "MM" + flag + "dd");
        String dateStr = dateFormat.format(date);
        return dateStr;
    }

    public static String formatDBDate(String date) {
        String replace = date.replace("2016", "") + "月";
        return replace;
    }

    public static String format2Double(Object obj) {
        return DOUBLE_FORMAT.format(Double.valueOf(obj.toString()));
    }

    public static String format2Float(Object obj) {
        return FLOAT_FORMAT.format(Float.valueOf(obj.toString()));
    }

    public static String format2Int(Object obj) {
        return INT_FORMAT.format(Integer.valueOf(obj.toString()));
    }

    public static String format(Object obj) {
        return NUMBER_FORMAT.format(Double.valueOf(obj.toString()));
    }
}
